package com.ylr.blog.framework.controller.web;

import java.util.List;
import java.util.Objects;

import com.ylr.base.common.annotation.Resubmit;
import com.ylr.base.common.dto.PageDto;
import com.ylr.blog.common.dto.audit_blog.AssignPageEnumDto;
import com.ylr.blog.common.dto.audit_blog.AuditBlogDetailDto;
import com.ylr.blog.common.dto.audit_blog.AuditBlogDto;
import com.ylr.user.common.dto.user.UserDto;
import com.ylr.blog.common.enums.AuditBlogFlowEnum;
import com.ylr.base.common.jsr303.group.OrderSearch;
import com.ylr.base.common.tool.Result;
import com.ylr.blog.common.vo.audit_blog.AssignVo;
import com.ylr.blog.common.vo.audit_blog.AuditBlogFormVo;
import com.ylr.blog.common.vo.audit_blog.AuditVo;
import com.ylr.blog.common.vo.blog.BlogVo;
import com.ylr.blog.framework.service.AuditBlogService;
import com.ylr.blog.framework.manager.AuditBlogManager;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-19 20:35:21
 * className: AuditBlogWebController
 * version: 1.0
 * description:
 */
@Validated
@RestController
@RequestMapping("/web/audit-blog")
@AllArgsConstructor
public class AuditBlogWebController {

    private static final Logger log = LoggerFactory.getLogger(AuditBlogWebController.class);

    private final AuditBlogService auditBlogService;

    private final AuditBlogManager auditBlogManager;


    /**
     * 保存待审核博客接口
     * 预留接口
     * <p>
     * 此接口会校验是否重复提交
     * @param blogVo 参数对象
     */
    @Resubmit(spEL = "'optAuditBlog'.concat(#blogVo.id)", message = "保存待审核博客")
    @PostMapping("/save")
    public Result<Object> saveAuditBlog(@Validated @RequestBody BlogVo blogVo) {
        log.info("web => 开始保存待审核博客操作。{}", blogVo);

        List<Long> tagIdList = blogVo.getTagIdList();
        int length = tagIdList.size();
        for (int i = 0; i < length; i++) {
            if (Objects.isNull(tagIdList.get(i))) {
                return Result.requestFail("博客关联博客标签，第" + (i + 1) + "个值错误");
            }
        }
        auditBlogManager.saveAuditBlog(blogVo);

        log.info("web => 保存待审核博客操作结束");
        return Result.success();
    }

    /**
     * 分页查询待审核博客接口
     * @param auditBlogFormVo 参数对象
     * @return 待审核博客分页数据
     */
    @PostMapping("/page/list")
    public Result<PageDto<AuditBlogDto>> pageAuditBlogList(@Validated(OrderSearch.class) @RequestBody AuditBlogFormVo auditBlogFormVo) {
        log.info("web => 开始分页查询待审核博客操作。{}", auditBlogFormVo);

        PageDto<AuditBlogDto> pageDto = auditBlogService.pageAuditBlogList(auditBlogFormVo);

        log.info("web => 分页查询待审核博客操作结束");
        return Result.success(pageDto);
    }


    /**
     * 获取待审核博客接口
     * @param id 主键
     * @return 待审核博客对象
     */
    @GetMapping("/get")
    public Result<AuditBlogDetailDto> getAuditBlog(@RequestParam("id") Long id, @RequestParam("code") String code) {
        log.info("web => 开始获取待审核博客操作。id={}", id);

        AuditBlogDetailDto auditBlogDetailDto = auditBlogManager.getAuditBlog(id, code);

        log.info("web => 获取待审核博客操作结束");
        return Result.success(auditBlogDetailDto);
    }

    /**
     * 获取被分单用户接口
     * @param id 主键
     * @return 被分单用户
     */
    @GetMapping("/getAuditUser")
    public Result<UserDto> getAuditUser(@RequestParam("id") Long id, @RequestParam("code") String code) {
        log.info("web => 开始获取被分单用户操作。id={}", id);

        UserDto userDto = auditBlogManager.getAuditUser(id, code);

        log.info("web => 获取被分单用户操作结束");
        return Result.success(userDto);
    }

    /**
     * 获取待审核博客分单页面枚举接口
     */
    @GetMapping("/page/enums")
    public Result<AssignPageEnumDto> getAssignPageEnum() {

        AssignPageEnumDto assignPageEnumDto = auditBlogService.getAssignPageEnum();

        return Result.success(assignPageEnumDto);
    }

    /**
     * 博客审核分单接口
     * 分单、委托、复审、转派
     * <p>
     * 此接口会校验是否重复提交
     * @param assignVo 参数对象
     */
    @Resubmit(spEL = "'assignAudit'.concat(#id)", message = "分单")
    @PostMapping("/assign")
    public Result<Object> assignAudit(@Validated @RequestBody AssignVo assignVo) {
        log.info("web => 开始博客审核分单操作。{}", assignVo);

        if (AuditBlogFlowEnum.isTypeFail(assignVo.getType())) {
            String msg = "分单类型值错误";
            log.info(msg);
            return Result.requestFail(msg);
        }

        auditBlogManager.assignAudit(assignVo);

        log.info("web => 博客审核分单操作结束");
        return Result.success();
    }

    /**
     * 提交博客审核结果接口
     * @param auditVo 参数对象
     */
    @Resubmit(spEL = "'commitAudit'.concat(#userPo.id)", message = "审核通过")
    @PostMapping("/audit")
    public Result<Object> commitAudit(@Validated @RequestBody AuditVo auditVo) {
        log.info("web => 开始提交博客审核操作。{}", auditVo);

        if (auditVo.isAuditReject() && StringUtils.isBlank(auditVo.getReply())) {
            log.info("审核拒绝，审核回复值错误");
            return Result.requestFail("审核拒绝，请输入审核回复内容");
        }

        auditBlogManager.commitAudit(auditVo);

        log.info("web => 提交博客审核操作结束");
        return Result.success();
    }

    /**
     * 修改待审核博客操作
     * @param blogVo 参数对象
     */
//    @Resubmit(spEL = "'optAuditBlog'.concat(#blogVo.id)", message = "保存待审核博客博客")
//    @PostMapping("/update")
//    public Result<Object> updateAuditBlog(@Validated @RequestBody BlogVo blogVo) {
//        log.info("web => 开始修改待审核博客操作。{}", blogVo);
//
//        List<Long> tagIdList = blogVo.getTagIdList();
//        int length = tagIdList.size();
//        for (int i = 0; i < length; i++) {
//            if (Objects.isNull(tagIdList.get(i))) {
//                return Result.requestFail("博客关联博客标签，第" + (i + 1) + "个值错误");
//            }
//        }
//        auditBlogService.updateAuditBlog(blogVo);
//
//        log.info("web => 修改待审核博客操作结束");
//        return Result.success();
//    }

    /**
     * 删除待审核博客接口
     * @param idList 主键列表
     */
//    @Resubmit(spEL = "'deleteAuditBlog'.concat(#userPo.id)", message = "删除待审核博客")
//    @DeleteMapping("/delete")
//    public Result<Object> deleteAuditBlog(@Size(min = 1, message = "主键列表不能为空") @RequestBody List<Long> idList) {
//        log.info("web => 开始删除待审核博客操作。idList={}", idList);
//
//        int length = idList.size();
//        for (int i = 0; i < length; i++) {
//            if (Objects.isNull(idList.get(i))) {
//                return Result.requestFail("主键列表，第" + (i + 1) + "个值错误");
//            }
//        }
//
//        auditBlogService.removeAuditBlog(idList);
//
//        log.info("web => 删除待审核博客操作结束");
//        return Result.success();
//    }

}
